package com.youxue.teachin.set.main;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import com.Navigator;
import com.library.uiframework.fragment.ptr.PagingRecyclerViewFragment;
import com.library.uiframework.mvp.PagingDataLoadPresenter;
import com.library.uiframework.utils.MToast;
import com.library.xtype.IAdapter;
import com.library.xtype.NuwaRecyclerViewAdapter;
import com.youxue.teachin.R;
import com.youxue.teachin.set.child.my.IChageUserView;
import com.youxue.teachin.set.child.my.UserChangePresener;
import com.youxue.teachin.set.main.binder.KeyValueBinder;
import com.youxue.teachin.set.main.binder.SheJiaoBinder;
import com.youxue.teachin.set.main.binder.TitleBinder;
import com.youxue.teachin.set.main.binder.UserHeadBinder;
import com.youxue.teachin.set.main.vo.KeyValueVO;
import com.youxue.teachin.set.repository.po.UserPO;
import com.youxue.teachin.user.userSystem.UserSystem;
import com.zaaach.citypicker.CityPickerActivity;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 21:32
 * @version: 9.1.0
 */
public class SetFragment extends PagingRecyclerViewFragment implements OnClickListener, IChageUserView {
    UserChangePresener mUserChangePresener;

    @Override
    protected IAdapter onCreateAdapter() {
        NuwaRecyclerViewAdapter adapter = new NuwaRecyclerViewAdapter();
        adapter.register(UserHeadBinder.class);
        adapter.register(TitleBinder.class);
        adapter.register(KeyValueBinder.class);
        adapter.register(SheJiaoBinder.class);
        adapter.setOnClickListener(this);
        return adapter;
    }

    @Override
    public boolean onInterceptLoadMore() {
        return true;
    }

    @Override
    public boolean onInterceptRefresh() {
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        onRetryRequested();
    }

    @Override
    protected PagingDataLoadPresenter onCreatePresenter() {
        return new SetPresenter(this);
    }

    String chooseCity;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            //区域设置
            chooseCity = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY);
            updateCity();
        }
    }

    private void updateCity() {
        if (mUserChangePresener == null) {
            mUserChangePresener = new UserChangePresener(this);
        }
        UserPO userVO = new UserPO();
        userVO.area = chooseCity;
        mUserChangePresener.updateUser(userVO);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.key_value_layout) {
            KeyValueVO vo = (KeyValueVO)v.getTag();
            if (vo.type == KeyValueVO.TYPE_ARREA) {
                new Navigator.Builder()
                    .setPageName("set_change_area")
                    .setRequestCode(this, 100)
                    .build().open();
            }
        }
    }

    @Override
    public void updateSucces() {
        if (chooseCity != null) {
            UserSystem.getInstance().getUserVO().area = chooseCity;
            chooseCity = null;
            onRetryRequested();
        }
    }

    @Override
    public void updateFail() {
        if (chooseCity != null) {
            chooseCity = null;
            MToast.getInstance().setText("更新区域失败");
        }
    }
}
